#ifndef FILEDIRHELPER_H
#define FILEDIRHELPER_H

#include <algorithm>
#include <string>
#include <vector>


namespace myLogger
{
	namespace Helper
	{

#if defined(_WIN32)
		std::string const dir_sep("\\");
#else
		std::string const dir_sep("/");
#endif

		//system specific separator.
		struct path_sep_comp : public std::unary_function < char, bool >
		{
			bool operator () (char ch) const
			{
#if defined (_WIN32)
				return ch == ('\\') || ch == ('/');
#else
				return ch == LOG4CPLUS_TEXT('/');
#endif
			}
		};

		struct is_empty_string : public std::unary_function < std::string const &, bool >
		{
			bool	operator () (std::string const & str) const
			{
				return str.empty();
			}
		};

		std::string	Tool_get_current_dir();

		template <typename PathSepPred, typename Container>
		static	void	Tool_split_into_components(
			Container & components,
			std::string const & path,
			PathSepPred is_sep = PathSepPred())
		{
			std::string::const_iterator const end = path.end();
			std::string::const_iterator it = path.begin();
			while (it != end)
			{
				std::string::const_iterator sep = std::find_if(it, end, is_sep);
				components.push_back(std::string(it, sep));
				it = sep;
				if (it != end)
					++it;
			}
		}

		template <typename PathSepPred, typename Container>
		void Tool_expand_relative_path(Container & components, PathSepPred is_sep = PathSepPred())
		{
			// Get the current working director.

			std::string const cwd = Tool_get_current_dir();

			// Split the CWD.

			std::vector<std::string> cwd_components;

			// Use qualified call to appease IBM xlC.
			Helper::Tool_split_into_components(cwd_components, cwd, is_sep);

			// Insert the CWD components at the beginning of components.

			components.insert(components.begin(), cwd_components.begin(), cwd_components.end());
		}

		template <typename Cont>
		void Tool_remove_empty(Cont & cont, std::size_t special)
		{
			cont.erase(std::remove_if(cont.begin() + special, cont.end(), is_empty_string()), cont.end());
		}

		//! Join a list of items into a string.
		template <typename Iterator>
		inline void Tool_join(std::string & result, Iterator start, Iterator last, std::string const & sep)
		{
			if (start != last)
				result = *start++;

			for (; start != last; ++start)
			{
				result += sep;
				result += *start;
			}
		}

		bool Tool_split_path(std::vector<std::string> & components, std::size_t & special, std::string const & path);

		bool Tool_CheckFileOrDirExsit(const char* dirName);

		long Tool_make_directory(std::string const & dir);

		void Tool_make_dirs(std::string const & file_path);

		size_t Tool_get_file_size(const char *FileName)
		{
			FILE* tmpFile = fopen(FileName, "rb");    
			//FILE* tmpFile = NULL;
			//errno_t errCode;
			//_set_errno(0);
			//errCode = fopen_s(&tmpFile, FileName, "rb");
			if (tmpFile)
			{
				//fseek(tmpFile, 0, SEEK_END);
				//long fileSize = ftell(tmpFile);
				//fclose(tmpFile);
				//tmpFile = NULL;
				//return fileSize;

				long fileSize = _filelength(_fileno(tmpFile));
				fclose(tmpFile);
				tmpFile = NULL;
				return fileSize;
			}
			else
			{
				//printf("Tool_GetFileSize, failed, error code = %d", errCode);
				return 0;
			}
		}
	}
}

#endif